<?php
/**
 * @author 		supfire
 * @copyright 	
 * @version 		1.0
 * @name 		msg.class.php
 * @todo 		处理消息操作
 * @final 		2009-05-18
 */
!defined("IN_YCTOOL") && exit();

class msg
{
    /*
            //发邮件 ccemail为0及时发送，sendok为0没有发送的
    */
    public static function Msendemail($userid, $username, $useremail, $message, $msgid, $iscut = 1, $isfoot = 1, $title = 0)
    {
	if($useremail)
        {
            //$iscut && $message = htmlSubString($message, 100);

            if($username)
            {
                $username = $username . '给您的信息';
            }

            $messageHtml = "童心网信息，".$username." ";
            if($title)
            {
                //$title = $message;
                $messageHtml .= '<br /><br /><br />标题:'.$title.'<br /><br />内容:';
            }
            $messageHtml .= $message;
            $messageHtml .= '
<br /><br /><br />
感谢您使用童心网';
            if($isfoot)
            {
                $messageHtml .= '
<br /><br />
如果您希望调控收取站外电子邮件的频率, 您可以在帐户的邮件联系页面进行设置, 谢谢! 
<br /><br />';
            }
            $detailhtml = '<META http-equiv=Content-Type content="text/html; charset='.Email_CHARSET.'"></HEAD>
                                    <br /><br />
                                    '.$messageHtml.'
                                    <br /><br />
                                    ';
            SendMail($useremail, '童心网信息', $detailhtml);
        }
        $msgid && db::updateRs(DB_TABLEPRE . 'msg_thread', 'sendok = 1', 'touid like \'%'.$userid.',%\' AND ccemail = 0  AND msgid = '. $msgid);
    }

    /**
    * 发送消息     istop 为 1 时为 主消息 ccount 为回复数
    * @param $touid			    接收者列表 userid 串 例如：1001,1002,1003, 或者 1001,
    * @param $fromuid			    发送者 fromuid  例如：1001 或者 1002		    
    * @param $message			    消息内容
    * @return bool
    * @author supfire
    * @final 2008-08-28
    */
    public static function sendMessage($touid, $fromuid, $title, $message, $nottitle = 0)
    {
	if($touid && $fromuid)
	{
		writeNewNote($touid, 1);
                
                require_once(DIR_COMM . 'cuthtml.func.php');
                require_once(DIR_COMM . 'sendmail.php');  

                $touid = explode(',', $touid);


                    foreach ($touid as $m)
                    {
                        if($m)
                        {
                            $m = $m . ',';

                            db::insertRs(DB_TABLEPRE . 'msg_thread', 'touid, fromuid, 
                                    title, message, istop, addtime', "'$m', $fromuid, '$title', '$message', 1, ". time());
                            $msgid = db::insertID();
                                    
                            //发邮件begin ccemail为0及时发送，sendok为0没有发送的
                            //$ccemail = $useremail = db::getOne(DB_TABLEPRE . 'msg_thread', 'ccemail', 'sendok = 0 AND msgid = '.$msgid);
                            //if($ccemail == 0)
                            //{
                                    $userrsusername = db::getOne(DB_TABLEPRE . 'user_basic', 'username', 'userid = '.$fromuid);
                                    $touids    = substr($m, 0, -1);
                                    $userrs = db::getAll(DB_TABLEPRE . 'user_basic', 'userid, email, ccemail', 'userid IN ('.$touids.')');
                                    
                                    foreach($userrs as $v)
                                    {
                                        if($v['ccemail'] == 0) 
                                        {
                                            $nottitle && $title = 0;
                                            self::Msendemail($v['userid'], $userrsusername, $v['email'], $message, 
                                                $msgid, $iscut = 1, $isfoot = 1, $title);
                                        }
                                        else
                                        {
                                            db::updateRs(DB_TABLEPRE . 'msg_thread', 'ccemail = '.$v['ccemail'], 
                                                'touid like \'%'.$v['userid'].',%\'  AND msgid = '. $msgid);
                                        }
                                    }
                            //}
                            //发邮件end	
                        } 
                    }

		return true;
	}
	else
	{
	return false;
	}
    }

    /**
     * 查询接受的消息列表
     * @param $userid
     * @return array $rs
     * @author supfire
     * @final 2008-08-24
     */
    public function toMeMessage($userid, $start, $perpage)
    {
	return db::getLeftJoin(DB_TABLEPRE . 'msg_thread', DB_TABLEPRE . 'user_basic', 
	    'b.username, a.msgid, a.parentid, a.fromuid, a.ccount, a.addtime, a.title,
            a.message, a.type, a.isreply','b.userid = a.fromuid', 
            'fromuid <> '.$userid.' AND a.isdel <> 2 AND a.touid like "%'.$userid.',%" ORDER BY a.addtime DESC', $start, $perpage);
    }

    /**
     * 查询接受的的消息条数
     * @param $userid 
     * @return int  
     * @author supfire
     * @final 2008-08-24
     */
    public function toMeMCount($userid)
    {
	return db::getCount(DB_TABLEPRE . 'msg_thread','msgid', 'fromuid <> '.$userid.' AND touid like "%'.$userid.',%" AND isdel <> 2');
    }

    /**
     * 查询我发送的的消息列表
     * @param $userid 
     * @return array $rs
     * @author supfire
     * @final 2008-08-24
     */
    public function fromMeMessage($userid, $start, $perpage)
    {
	return db::getLimit(DB_TABLEPRE . 'msg_thread','touid, msgid, ccount, addtime, parentid, title,
	    message,type,isreply', "fromuid = $userid AND isdel <> 1 ORDER BY addtime DESC", $start, $perpage);	
    }

    /**
     * 查询我发送的的消息条数
     * @param $userid 
     * @return int  
     * @author supfire
     * @final 2008-08-24
     */
    public function fromMeMCount($userid)
    {
	return db::getCount(DB_TABLEPRE . 'msg_thread','msgid', "fromuid = $userid AND isdel <> 1");
    }

    /**
    * 回复短消息    istop 为 0 时为回复消息 perantid 为父ID
    * @param $perantid			主消息ID msgid
    * @param $touid			接收者列表 userid 串 例如：1001,1002,1003, 或者 1001,
    * @param $fromuid			发送者 fromuid  例如：1001 或者 1002		    
    * @param $message			消息内容
    * @return bool
    * @author supfire
    * @final 2008-08-28
    */
    public static function sendComment($msgid, $touid, $fromuid, $title, $message)
    {
	$ccount = db::getOne(DB_TABLEPRE . 'msg_thread','ccount', 
	    'msgid = '.$msgid.' OR parentid = '.$msgid.' ORDER BY addtime DESC' ) + 1;

	//db::updateRs(DB_TABLEPRE . 'msg_thread', 'istop = 1', 'msgid = '.$msgid.' OR parentid = '.$msgid  );

	$isOk = db::insertRs(DB_TABLEPRE . 'msg_thread', 'parentid, touid, fromuid, title,
	    message, ccount, addtime', "$msgid, '$touid', $fromuid, '$title', '$message', $ccount, ". time());
	$msgid = db::insertID();
	//发邮件begin ccemail为0及时发送，sendok为0没有发送的
	//$ccemail = $useremail = db::getOne(DB_TABLEPRE . 'msg_thread', 'ccemail', 'sendok = 0 AND msgid = '.$msgid);
	//if($ccemail == 0)
	//{
		
		require_once(DIR_COMM . 'cuthtml.func.php');
		require_once(DIR_COMM . 'sendmail.php');  
		$userrs = db::getRow(DB_TABLEPRE . 'user_basic', 'username,email', 'userid = '.$fromuid);
		$userrsusername = $userrs[username];
		$touids    = substr($touid, 0, -1);
                $userrsK = db::getAll(DB_TABLEPRE . 'user_basic', 'userid, email, ccemail', 'userid IN ('.$touids.')');

		//$touidrs = explode(',', $touids);
		foreach($userrsK as $v)
                {                    
                    if($v['ccemail'] == 0) 
                    {
                        if($v['userid'] != $fromuid)
                        {
                            self::Msendemail($v['userid'], $userrsusername, $v['email'], $message, 
                                $msgid, $iscut = 1, $isfoot = 1, $title);
                        }
                    }
                    else
                    {
                        db::updateRs(DB_TABLEPRE . 'msg_thread', 'ccemail = '.$v['ccemail'], 
                            'touid like \'%'.$v['userid'].',%\'  AND msgid = '. $msgid);
                    }
                }



	//}
	//发邮件end	
	
	$touid = str_replace($fromuid.',', '', $touid);
	writeNewNote($touid, 1);
	return $msgid;
    }

    /**
     * 查询某一条消息列表
     * @param $userid 
     * @return array $rs
     * @author supfire
     * @final 2008-08-24
     */
    public function oneMsgList($msgid, $start, $perpage, $userid = 0, $mid)
    {
        $addtime = db::getOne(DB_TABLEPRE . 'msg_thread', 'addtime', 'msgid = '.$mid );
        if($userid)
        {
            $isdel = "AND ((a.fromuid <> $userid AND a.isdel <> 2 AND a.touid like '%".$userid.",%') OR (a.fromuid = $userid AND a.isdel <> 1)) ";
        }

        $isdel .= " AND a.addtime <= '$addtime' ";

	return db::getLeftJoin(DB_TABLEPRE . 'msg_thread', DB_TABLEPRE . 'user_basic', 
	    'b.username, a.touid, a.msgid, a.fromuid, a.ccount, a.addtime, a.title,
            a.message','b.userid = a.fromuid', 
            '(a.parentid = '.$msgid.' OR a.msgid = '.$msgid.') 
            '.$isdel.' ORDER BY a.addtime DESC', $start, $perpage);
     }

    /**
     * 查询某一条回复短消息条数
     * @param $msgid 
     * @return int  
     * @author supfire
     * @final 2008-08-24
     */
    public function oneMsgCount($msgid, $mid = 0)
    {
        $addtime = db::getOne(DB_TABLEPRE . 'msg_thread', 'addtime', 'msgid = '.$mid );
        if($fromto == 'from')
        {
            $isdel = " AND isdel <> 1 ";
        }
        else
        {
            $isdel = " AND isdel <> 2 ";
        }

        $isdel .= " AND a.addtime <= '$addtime' ";

        return db::getCount(DB_TABLEPRE . 'msg_thread', 'msgid', 
            $isdel.' (parentid = '.$msgid.' OR msgid = '.$msgid .')');
    }

    //删除
    public function deleteFT($userid, $msgid, $all = 0)
    {
        if($all == 1)
        {
            $rsall = db::getAll(DB_TABLEPRE . 'msg_thread', 'msgid, isdel, fromuid', 
                "msgid = $msgid OR parentid = $msgid ORDER BY addtime DESC" );

            if($rsall)
            {
                foreach ($rsall as $v) 
                {
                    $fromuid    = $v['fromuid'];
                    if($userid == $fromuid)
                    {
                        $isdel = 1;
                        if($v['isdel'] == 2)
                        {
                            db::deleteRs(DB_TABLEPRE . 'msg_thread', 'msgid = '.$v['msgid']);
                        }
                        else
                        {
                            db::updateRs(DB_TABLEPRE . 'msg_thread', 'isdel = '.$isdel, 'msgid = '.$v['msgid'] );
                        }
                    }
                    else
                    {
                        $isdel = 2;
                        if($v['isdel'] == 1)
                        {
                            db::deleteRs(DB_TABLEPRE . 'msg_thread', 'msgid = '.$v['msgid']);
                        }
                        else
                        {
                            db::updateRs(DB_TABLEPRE . 'msg_thread', 'isdel = '.$isdel, 'msgid = '.$v['msgid']);
                        }
                    }
                }
            } 
            return true;

        }
        //(a.fromuid <> 1005 AND a.isdel <> 2 AND a.touid like '%1005,%') 
        //OR (a.fromuid = 1005 AND a.isdel <> 1) 

        $rs    = db::getRow(DB_TABLEPRE . 'msg_thread', 'touid, isdel, fromuid', 
            "msgid = $msgid ORDER BY addtime DESC" );
        $fromuid    = $rs['fromuid'];
        if($userid == $fromuid)
        {
            $isdel = 1;
            if($rs['isdel'] == 2)
            {
                return db::deleteRs(DB_TABLEPRE . 'msg_thread', 'msgid = '.$msgid);
            }
            else
            {
                return db::updateRs(DB_TABLEPRE . 'msg_thread', 'isdel = '.$isdel, 'msgid = '.$msgid );
            }
        }
        else
        {
            $isdel = 2;
            if($rs['isdel'] == 1)
            {
                return db::deleteRs(DB_TABLEPRE . 'msg_thread', 'msgid = '.$msgid);
            }
            else
            {
                return db::updateRs(DB_TABLEPRE . 'msg_thread', 'isdel = '.$isdel, 'msgid = '.$msgid);
            }
        }
    }

    /**
     * 通过 userid,msgid 删除我接受的某条消息
     * @author supfire
     * @final 2008-08-24
     */
    public function deleteToMe($userid, $msgid)
    {
	$rs    = db::getRow(DB_TABLEPRE . 'msg_thread', 'touid, isdel', 'touid like "%'.$userid.',%" AND (msgid = '.$msgid.' OR parentid = '.$msgid.') ORDER BY addtime DESC' );
	$touid    = str_replace($userid.',', '', $rs['touid']);
	if($touid)
	{
	    return db::updateRs(DB_TABLEPRE . 'msg_thread', "touid = '$touid'", 'msgid = '.$msgid.' OR parentid = '.$msgid  );
	}
	else
	{
	    if($rs['isdel'] == 1)
	    {
		return db::deleteRs(DB_TABLEPRE . 'msg_thread', 'msgid = '.$msgid.' OR parentid = '.$msgid);
	    }
	    else
	    {
		return db::updateRs(DB_TABLEPRE . 'msg_thread', 'isdel = 2', 'msgid = '.$msgid.' OR parentid = '.$msgid);
	    }
	}
    }

    /**
     * 通过 userid 删除我发送的所有消息
     * @author supfire
     * @final 2008-08-24
     */
    public function deleteToMeAll($userid)
    {
	$rs    = db::getAll(DB_TABLEPRE . 'msg_thread', 'msgid, touid, isdel', 'touid like "%'.$userid.',%"  AND isdel <> 2' );
	foreach ($rs as $i => $value)
	{
	    $touid    = str_replace($userid.',', '', $value['touid']);
	    if($touid)
	    {
		$isOk .= db::updateRs(DB_TABLEPRE . 'msg_thread', "touid = '$touid'", 'touid like "%'.$userid.',%"');
	    }
	    else
	    {
		if($value['isdel'] == 1)
		{
		    $isOk .= db::deleteRs(DB_TABLEPRE . 'msg_thread', 'msgid = '.$value['msgid'].' OR parentid = '.$value['msgid']);
		}
		else
		{
		    $isOk .= db::updateRs(DB_TABLEPRE . 'msg_thread', 'isdel = 2', 'msgid = '.$value['msgid'].' OR parentid = '.$value['msgid']);
		}
	    }
	}
	return $isOk;
    }

    /**
     * 通过 userid msgid 删除我发送的某条消息
     * @author supfire
     * @final 2008-08-24
     */
    public function deleteFromMe($userid, $msgid)
    {
	$isdel    = db::getOne(DB_TABLEPRE . 'msg_thread', 'isdel', 'fromuid = '.$userid.' AND msgid = '.$msgid.' OR parentid = '.$msgid );
	if($isdel == 2)
	{
	    return db::deleteRs(DB_TABLEPRE . 'msg_thread', 'msgid = '.$msgid.' OR parentid = '.$msgid);
	}
	else
	{
	    return db::updateRs(DB_TABLEPRE . 'msg_thread',  'isdel = 1', 'fromuid = '.$userid.' AND msgid = '.$msgid.' OR parentid = '.$msgid);
	}
    }

    /**
     * 通过 userid 删除我发送的所有消息
     * @author supfire 
     * @final 2008-08-24
     */
    public function deleteFromMeAll($userid)
    {
	$rs    = db::getAll(DB_TABLEPRE . 'msg_thread', 'isdel, msgid', 'fromuid = '.$userid.' AND isdel <> 1');

	foreach ($rs as $i => $value)
	{
	    if($value['isdel'] == 2)
	    {
		$isOk .= db::deleteRs(DB_TABLEPRE . 'msg_thread', 'msgid = '.$value['msgid'].' OR parentid = '.$value['msgid']);
	    }
	    else
	    {
		$isOk .= db::updateRs(DB_TABLEPRE . 'msg_thread',  'isdel = 1', 'msgid = '.$value['msgid'].' OR parentid = '.$value['msgid']);
	    }
	}
	return $isOk;
    }

    /**
    * 通过 $strvalue 添加动作
    * @param $strvalue 包括：$userid 接收者, $friendid 发送者, $type, $detail
    *    例如：msg::insertAction("($userid, $friendid, $type, '这里是所要显示的信息', ".time().", '备注信息')");
    *    
    *    例如：msg::insertAction('1001,1002,1,'这里是所要显示的信息',1212080900','备注信息'), ('1001,1002,1,'这里是所要显示的信息',1212080900','备注信息')
    *		msg::insertAction("$userid, $friendid, $type, '$message', ".time()." ), ("$userid, $friendid, $type, '$message', ".time()." )
    *
    * @return bool
    * @author supfire
    * @final 2008-08-28
    */
    public static function insertAction($strvalue)
    {
	return db::insertLotRs(DB_TABLEPRE . 'msg_action','userid, 
	    friendid, type, message, addtime, note', $strvalue);
    }

    /**
     * 通过 userid 获取好友动态
     * @author supfire 
     * @final 2008-08-25
     */
    public function getMsgAction($inUserid, $whereSql = 1, $permun=60, $page=0)
    {
	return db::getLeftJoin(DB_TABLEPRE . 'msg_action', DB_TABLEPRE . 'user_basic', 'a.userid, b.username, b.face, 
		a.friendid, a.type, a.message, a.addtime, a.note', 'a.userid = b.userid', '(a.userid IN('.$inUserid.')
	       	OR a.friendid IN('.$inUserid.')) AND '.$whereSql.' ORDER BY a.addtime DESC', $page, $permun);
    }

    /**
    * 通过 $strvalue 添加系统等消息
    * @param $userid 接收者, 
    *          $type 消息类型,
    *		     1 为 系统消息
    *		     X
    *		     3 为评论
    *		     4 为回复评论
    *		    11 礼物
    *		    12 动Ta一下
    *		    $message 消息内容
    * @return bool
    * @author supfire
    * @final 2008-08-28
    */
    public static function insertMsgSys($userid, $friendid, $type, $typeNote, $message, $userpathpre = '')
    {
	//writeNewNote($userid.',', $typeNote, $userpathpre);
	return db::insertRs(DB_TABLEPRE . 'msg_sys','userid, friendid,
	     type, message, addtime', "$userid, $friendid, $type, '$message', ". time());
    }

    /**
     * 查询接受的系统消息列表
     * @param $userid
     * @return array $rs
     * @author supfire
     * @final 2008-08-24
     */
    public function msgSys($userid, $type, $start, $perpage)
    {
	return db::getLimit(DB_TABLEPRE . 'msg_sys','*', "userid = $userid AND $type ORDER BY addtime DESC", $start, $perpage);	
    }

    /**
     * 查询接受的的系统消息条数
     * @param $userid 
     * @return int  
     * @author supfire
     * @final 2008-08-24
     */
    public function msgSysCount($userid, $type)
    {
	return db::getCount(DB_TABLEPRE . 'msg_sys','sysid', 'userid =  '.$userid.' AND '.$type);
    }

    /**
     * 通过 userid sysid 删除我的的某条系统消息
     * @author supfire
     * @final 2008-08-24
     */
    public function deleteSysOne($userid, $sysid)
    {
	return db::deleteRs(DB_TABLEPRE . 'msg_sys', 'sysid = '.$sysid.' AND userid = '.$userid);
    }

    /**
     * 通过 userid sysid 删除我所有的系统消息
     * @author supfire
     * @final 2008-08-24
     */
    public function deleteSysMsg($userid, $type)
    {
	return db::deleteRs(DB_TABLEPRE . 'msg_sys', 'userid = '.$userid.' AND '.$type);
    }

    /**
     * 处理特条系统消息 
     * @param $sysid 
     * @return int  
     * @author supfire
     * @final 2008-08-24
     */
    public function isSysMsg($userid, $sysid, $isreply)
    {
	return db::updateRs(DB_TABLEPRE . 'msg_sys', 'isreply = '.$isreply, 'sysid =  '.$sysid.' AND userid = '.$userid);
    }

    /**
     * 处理用户和好友之间的系统消息 
     * @param $sysid 
     * @return int  
     * @author supfire
     * @final 2008-08-24
     */
    public function isnoSysMsg($userid, $friendid, $isreply)
    {
	return db::updateRs(DB_TABLEPRE . 'msg_sys', 'isreply = '.$isreply, 'friendid = '.$friendid.' AND userid = '.$userid);
    }
}
?>
